package org.voidness.oje2d.gui;

import org.voidness.oje2d.Engine;
import org.voidness.oje2d.GLColor;
import org.voidness.oje2d.GLImage;

/** A button widget to use with the GUI engine. */
public class Button extends Widget {
    /** The normal image index */
    public static int NORMAL = 0;

    /** The over image index */
    public static int OVER = 1;

    /** The pressed image index */
    public static int PRESSED = 2;

    /** The images for the 3 images (normal, over and clicked */
    private GLImage[] images = null;

    /**
     * Constructor for the button class.
     * 
     * @param mWidth The button's width
     * @param mHeight The button's height
     */
    public Button(int mWidth, int mHeight) {
        // Call the widget's constructor
        super(mWidth, mHeight);

        // Set this widget in 0,0 relative to it's parent
        xPos = 0;
        yPos = 0;
    }

    /**
     * A more complex constructor for the button.
     * 
     * @param mX The button's position in the X axis
     * @param mY The button's position in the Y axis
     * @param mWidth The button's width
     * @param mHeight The button's height
     */
    public Button(int mX, int mY, int mWidth, int mHeight) {
        super(mX, mY, mWidth, mHeight);
    }

    /**
     * Defines the images for the button.
     * 
     * @param mImages A GLImage array with the button's images
     */
    public void setImages(GLImage[] mImages) {
        images = mImages;
    }

    /**
     * Draws the widget and all sub-widgets.
     */
    public void draw(GLColor mColor) {
        // Draw only if visible
        if (!visible)
            return;

        for (int i = 0; i < images.length; i++)
            images[i].setColor(mColor);

        // Check which state is the button in
        if (hasMouseOver() && Engine.getInstance().getMouseLeftButtonDown())
            images[PRESSED].draw(xPos + parent.getXPos(), yPos
                    + parent.getYPos());
        else if (hasMouseOver())
            images[OVER].draw(xPos + parent.getXPos(), yPos + parent.getYPos());
        else
            images[NORMAL].draw(xPos + parent.getXPos(), yPos
                    + parent.getYPos());

        // Cycle the widget list and draw all
    }
}